package com.cxq;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * Created by cxq on 2020/7/26.
 */
public class ThreeThreadStart {

    //表示参与同时启动的线程数
    private CyclicBarrier cyclicBarrier = new CyclicBarrier(3);

    public void startThread(){
        //打印线程准备启动
        String name = Thread.currentThread().getName();
        System.out.println(name+"正在准备");
        //电泳CyclicBarrier的await方法线程全部准备完成
        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        //打印线程启动完毕信息
        System.out.println(name+"线程已经启动完毕。"+"启动毫秒数："+new Date().getTime());
    }

    public static void main(String[] args) {
        ThreeThreadStart threeThreadStart = new ThreeThreadStart();

        new Thread(()->threeThreadStart.startThread(),"线程1").start();
        new Thread(()->threeThreadStart.startThread(),"线程2").start();
        new Thread(()->threeThreadStart.startThread(),"线程3").start();
    }
}
